package cn.ticket.controller;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ticket.model.Constants;
import cn.ticket.model.Refundlog;
import cn.ticket.service.ExcelExportService;
import cn.ticket.service.RefundLogService;
import cn.ticket.service.Utility;
import cn.ticket.service.WorkTicketOrderService;

@Controller
@RequestMapping("refundLog")
public class RefundLogController {
@Autowired
RefundLogService refundLogService;
@Autowired
WorkTicketOrderService workTicketOrderService;
@Autowired
ExcelExportService excelExportService;
@RequestMapping(value="insert",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String insert(@RequestBody Refundlog refundlog){
	Map<String, Object> result=new HashMap<>();
	int num =refundLogService.insertSelective(refundlog);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="delete",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String delete(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int id=Integer.parseInt(args.get("id"));
	int num =refundLogService.deleteByPrimaryKey(id);
	if (num==1) {
		result.put("status", 1001);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getAll",produces={"application/json;charset=utf-8"})
@ResponseBody
public String getAll(){
	Map<String, Object> result=new HashMap<>();
	List<Refundlog> refundlogs=refundLogService.getAll();
	if (!refundlogs.isEmpty()) {
		result.put("status", 1001);
		result.put("body", refundlogs);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}
@RequestMapping(value="getByCount",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
@ResponseBody
public String getByCount(@RequestBody Map<String, String> args){
	Map<String, Object> result=new HashMap<>();
	int start =Integer.parseInt(args.get("start"));
	int count=Integer.parseInt(args.get("count"));
	List<Refundlog> refundlogs=refundLogService.getByCount(start, count);
	if (!refundlogs.isEmpty()) {
		result.put("status", 1001);
		result.put("body", refundlogs);
	}
	else {
		result.put("status", 1002);
	}
	return Utility.gson.toJson(result);
}

@RequestMapping(value="exportExcelByDateRange")
@ResponseBody
public void exportExcelByDateRange(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException{
//	String startDate=request.getParameter("startDate");
//	String endDate=request.getParameter("endDate");
	String startDate="2017-05-01 00:00:00";
	String endDate="2017-05-30 00:00:00";
	List<Map<String, Object>> refundlogs=refundLogService.getRefundInfoByDateRange(startDate, endDate);
	String docsPath = request.getSession().getServletContext().getRealPath("/");
	final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");
	String[] headers = { "订票人", "乘坐人", "路线", "乘坐信息", "退票时间", "退票缘由"};
	OutputStream out = new FileOutputStream(docsPath + FILE_SEPARATOR + "tuipiao.xlsx");
	XSSFWorkbook workbook = new XSSFWorkbook();
	excelExportService.produceRefundLog("退票记录", headers, refundlogs, workbook);
//	excelService.produceExceldataPosChargeData("收费明细", headers, posdatas, workbook);
	try {
		workbook.write(out);
	} catch (IOException e) {
		e.printStackTrace();
	}
	Utility.download(docsPath + FILE_SEPARATOR + "tuipiao.xlsx", response);
}


@RequestMapping(value="exportExcelWorkTicketByDateRange")
@ResponseBody
public void exportExcelWorkTicketByDateRange(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, ParseException{
//	String startDate=request.getParameter("startDate");
//	String endDate=request.getParameter("endDate");
	String date="2018-04-13 06:50:00";
	String startdate="2018-04-08 06:50:00";
	String enddate="2018-05-30 06:50:01";
//	List<Map<String, Object>> workTicketOrders=workTicketOrderService.getInfoByDepartureDate(new SimpleDateFormat(Constants.DATEFORMAT).parse(date));
	List<Map<String, Object>> workTicketOrders=workTicketOrderService.getInfoByDepartureDateRange(new SimpleDateFormat(Constants.DATEFORMAT).parse(startdate), new SimpleDateFormat(Constants.DATEFORMAT).parse(enddate));
	if (workTicketOrders.isEmpty()) {
		return;
	}
	String docsPath = request.getSession().getServletContext().getRealPath("/");
	final String FILE_SEPARATOR = System.getProperties().getProperty("file.separator");
	String[] headers = { "订票人", "科室", "座位号", "订票时间","路线"};
	OutputStream out = new FileOutputStream(docsPath + FILE_SEPARATOR + "tuipiao.xlsx");
	XSSFWorkbook workbook = new XSSFWorkbook();
	excelExportService.produceWorkTicketOrder("订票记录", headers, workTicketOrders, workbook);
//	excelService.produceExceldataPosChargeData("收费明细", headers, posdatas, workbook);
	try {
		workbook.write(out);
	} catch (IOException e) {
		e.printStackTrace();
	}
	Utility.download(docsPath + FILE_SEPARATOR + "tuipiao.xlsx", response);
}
}
